/*
 * MIT License
 *
 * Copyright (c) 2023 芊芊墨客
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.zhangmeng.tools.controller;

import com.zhangmeng.tools.utils.ImagePath;
import com.zhangmeng.tools.utils.ResourcesUtils;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URL;
import java.util.Arrays;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-02-18 14:43
 */
@Slf4j
public class SmallToolsController {

    private SimpleDoubleProperty width = new SimpleDoubleProperty(0.0);
    private SimpleDoubleProperty height = new SimpleDoubleProperty(0.0);
    private AnchorPane root;

    private AnchorPane hex_16;
    private AnchorPane unicode;
    private AnchorPane jwt_web;
    private AnchorPane color_choose;
    private AnchorPane qr_code;
    private AnchorPane date_query;
    private AnchorPane cron;
    private AnchorPane mail;
    private AnchorPane telephone;
    private AnchorPane json_view;
    private AnchorPane maven_install_jar;
    private AnchorPane word_ocr;
    private AnchorPane bar_code;
    private AnchorPane pdf_ocr;
    private AnchorPane batch_update_file_name;
    private AnchorPane capter_screen;
    private AnchorPane file_edit;
    private AnchorPane layui_form_gen;
    private AnchorPane log_console;
    private AnchorPane editPlusCode;
    private AnchorPane minioUpload;
    private AnchorPane excel_read;

    @FXML
    private ListView<ResourcesUtils.SmallTools> listView;

    @FXML
    private SplitPane splitPane;

    public static final String color_cell = "#f4f4f4";

    @FXML
    public void md5_menu_item() {
        load_encrypt(0);
    }

    @FXML
    public void spring_security_menu_item() {
        load_encrypt(1);
    }

    @FXML
    public void video_menu_item() {
        load_player(0);
    }

    @FXML
    public void music_menu_item() {
        load_player(1);
    }

    @FXML
    public void vip_parser_menu_item() {
        load_player(2);
    }

    @FXML
    public void cron_menu_item() {
        boolean flag = false;
        if (cron != null) {
            flag = true;
        }

        cron(flag);
    }

    @FXML
    public void unicode_menu_item() {

        boolean flag = false;
        if (unicode != null) {
            flag = true;
        }

        unicode(flag);
    }

    @FXML
    public void jwt_menu_item() {
        boolean flag = false;
        if (jwt_web != null) {
            flag = true;
        }

        jwt_web(flag);
    }

    @FXML
    public void hex_16_menu_item() {
        boolean flag = false;
        if (hex_16 != null) {
            flag = true;
        }
        hex_16(flag);
    }

    @FXML
    public void color_choose_menu_item() {

        boolean flag = false;
        if (color_choose != null) {
            flag = true;
        }
        color_choose(flag);
    }

    @FXML
    public void qr_code_menu_item() {

        boolean flag = false;
        if (qr_code != null) {
            flag = true;
        }
        qr_code(flag);
    }

    @FXML
    public void date_query_menu_item() {
        boolean flag = false;
        if (date_query != null) {
            flag = true;
        }
        date_query(flag);
    }

    @FXML
    public void base_62_menu_item() {
        load_codec_tools(0);
    }

    @FXML
    public void base_64_menu_item() {
        load_codec_tools(1);
    }

    @FXML
    public void base_32_menu_item() {
        load_codec_tools(2);
    }

    @FXML
    public void sql_code_gen_menu_item() {
        load_mysql_tools(0);
    }

    @FXML
    public void netty_client_menu_item() {
        load_network_tools(0);
    }

    public void load_network_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("network-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    @FXML
    public void morse_coder_menu_item() {
        load_codec_tools(3);
    }

    public void load_mysql_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("sql-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void load_codec_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("codec-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void load_player(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("player"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void load_encrypt(int index) {

        Stage stage = (Stage) splitPane.getScene().getWindow();

        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("home"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Scene scene = new Scene(fx);
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    @FXML
    public void initialize() {
        init();
        listView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                boolean flag = false;
                if (newValue.getIndex() == 0) {
                    if (hex_16 != null) {
                        flag = true;
                    }
                    hex_16(flag);
                }
                if (newValue.getIndex() == 1) {
                    if (unicode != null) {
                        flag = true;
                    }
                    unicode(flag);
                }

                if (newValue.getIndex() == 2) {
                    if (jwt_web != null) {
                        flag = true;
                    }
                    jwt_web(flag);
                }

                if (newValue.getIndex() == 3) {
                    if (color_choose != null) {
                        flag = true;
                    }
                    color_choose(flag);
                }
                if (newValue.getIndex() == 4) {
                    if (qr_code != null) {
                        flag = true;
                    }
                    qr_code(flag);
                }

                if (newValue.getIndex() == 5) {
                    if (date_query != null) {
                        flag = true;
                    }
                    date_query(flag);
                }

                if (newValue.getIndex() == 6) {
                    if (cron != null) {
                        flag = true;
                    }
                    cron(flag);
                }

                if (newValue.getIndex() == 7) {
                    if (mail != null) {
                        flag = true;
                    }
                    mail(flag);
                }
                if (newValue.getIndex() == 8) {
                    if (telephone != null) {
                        flag = true;
                    }
                    telephone(flag);
                }
                if (newValue.getIndex() == 9) {
                    if (json_view != null) {
                        flag = true;
                    }
                    json_view(flag);
                }
                if (newValue.getIndex() == 10) {
                    if (maven_install_jar != null) {
                        flag = true;
                    }
                    maven_install_jar(flag);
                }

                if (newValue.getIndex() == 11) {
                    if (word_ocr != null) {
                        flag = true;
                    }
                    word_ocr(flag);
                }

                if (newValue.getIndex() == 12) {
                    if (bar_code != null) {
                        flag = true;
                    }
                    bar_code(flag);
                }

                if (newValue.getIndex() == 13) {
                    if (pdf_ocr != null) {
                        flag = true;
                    }
                    pdf_ocr(flag);
                }

                if (newValue.getIndex() == 14) {
                    if (batch_update_file_name != null) {
                        flag = true;
                    }
                    batch_update_file_name(flag);
                }

                if (newValue.getIndex() == 15) {
                    if (capter_screen != null) {
                        flag = true;
                    }
                    capter_screen(flag);
                }

                if (newValue.getIndex() == 16) {
                    if (file_edit != null) {
                        flag = true;
                    }
                    file_edit(flag);
                }
                if (newValue.getIndex() == 17) {
                    if (layui_form_gen != null) {
                        flag = true;
                    }
                    layui_form_gen(flag);
                }
                if (newValue.getIndex() == 18) {
                    if (log_console != null) {
                        flag = true;
                    }
                    log_console(flag);
                }

                if (newValue.getIndex() == 19) {
                    if (editPlusCode != null) {
                        flag = true;
                    }
                    editPlusCode(flag);
                }

                if (newValue.getIndex() == 20) {
                    if (minioUpload != null) {
                        flag = true;
                    }
                    minioUpload(flag);
                }

                if (newValue.getIndex() == 21){
                    if (excel_read != null) {
                        flag = true;
                    }
                    excel_read(flag);
                }
            }
        });
    }

    private void excel_read(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(21);
        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("excel-read"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            excel_read = root;
        } else {
            root = excel_read;
        }
        common_method();
    }

    private void minioUpload(boolean flag) {

        //默认选择第一个
        listView.getSelectionModel().select(20);
        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("minio-upload"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            minioUpload = root;
        } else {
            root = minioUpload;
        }
        common_method();
    }

    private void editPlusCode(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(19);
        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("edit_plus_code"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            editPlusCode = root;
        } else {
            root = editPlusCode;
        }
        common_method();
    }

    private void log_console(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(18);
        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("log-console"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            log_console = root;
        } else {
            root = log_console;
        }
        common_method();
    }

    private void layui_form_gen(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(17);
        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("layui-form"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            layui_form_gen = root;
        } else {
            root = layui_form_gen;
        }
        common_method();
    }

    private void file_edit(boolean flag) {

        //默认选择第一个
        listView.getSelectionModel().select(0);
        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("file-edit"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            file_edit = root;
        } else {
            root = file_edit;
        }
        common_method();
    }


    public static Image getImage(ResourcesUtils.SmallTools SmallTools) {
        return switch (SmallTools) {
            case Hex_16 -> new Image(ImagePath.path(ImagePath.ImagePathType.Hex_16));
            case Unicode -> new Image(ImagePath.path(ImagePath.ImagePathType.Unicode));
            case JWT_WEB -> new Image(ImagePath.path(ImagePath.ImagePathType.JWT_WEB));
            case COLOR_CHOOSE -> new Image(ImagePath.path(ImagePath.ImagePathType.COLOR_CHOOSE));
            case Qr_CODE -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
            case Date_Query -> new Image("image/日历.png");
            case Cron -> new Image("image/定时.png");
            case Mail -> new Image("image/mail.png");
            case TelePhone -> new Image("image/手机号.png");
            case JSONView -> new Image("image/json.png");
            case Maven_Install_Jar -> new Image("image/Maven.png");
            case Word_ocr -> new Image("image/OCR.png");
            case Bar_Code -> new Image("image/条形码.png");
            case Pdf_Ocr -> new Image("image/pdf.png");
            case BatchUpdateFileName -> new Image("image/批量修改.png");
            case Capter_Screent -> new Image("image/截图.png");
            case File_Edit -> new Image("image/编辑.png");
            case LayUI_Form_Gen -> new Image("image/layui.png");
            case LogConsole -> new Image("image/layui.png");
            case EditPlusCode -> new Image("image/layui.png");
            case MinioUpload -> new Image("image/layui.png");
            case ExcelRead -> new Image("image/layui.png");
        };
    }

    public void init() {
        ResourcesUtils.SmallTools[] values = ResourcesUtils.SmallTools.values();
        ObservableList<ResourcesUtils.SmallTools> list = FXCollections.observableArrayList();
        list.addAll(Arrays.asList(values));
        listView.setItems(list);
        listView.setFixedCellSize(40);
        listView.setCellFactory(new Callback<>() {

            private int position;

            @Override
            public ListCell<ResourcesUtils.SmallTools> call(ListView<ResourcesUtils.SmallTools> SmallToolsListView) {

                Label label = new Label();
                label.setPrefWidth(200);
                ListCell<ResourcesUtils.SmallTools> listCell = new ListCell<>() {
                    @Override
                    protected void updateItem(ResourcesUtils.SmallTools SmallTools, boolean b) {
                        super.updateItem(SmallTools, b);
                        if (!b) {
                            HBox hBox = new HBox(30);
                            hBox.setAlignment(Pos.CENTER);
                            label.setText(SmallTools.getTitle());
                            label.setTextFill(Paint.valueOf("#000000"));
                            Image im = getImage(SmallTools);
                            ImageView iv = new ImageView(im);
                            iv.setPreserveRatio(true);
                            iv.setFitWidth(15);
                            hBox.getChildren().add(iv);
                            hBox.getChildren().add(label);
                            this.setGraphic(hBox);
                        }
                        this.setStyle("-fx-background-color: " + color_cell);
                    }
                };

                listCell.hoverProperty().addListener(new ChangeListener<Boolean>() {
                    @Override
                    public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                        if (t1 && !label.getText().equals("")) {
                            position = SmallToolsListView.getItems().indexOf(label.getText());
                            label.setFont(new Font(16));
                            SmallToolsListView.getFocusModel().focus(position);
                            listCell.setStyle("-fx-background-color: #369e7d");
                        } else {
                            label.setPrefHeight(20);
                            label.setFont(new Font(13));
                            listCell.setStyle("-fx-background-color: " + color_cell);
                        }
                    }
                });

                return listCell;
            }
        });
        hex_16(false);
    }


    private void hex_16(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(0);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("hex_16"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            hex_16 = root;
        } else {
            root = hex_16;
        }
        common_method();
    }

    private void unicode(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(1);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("unicode"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            unicode = root;
        } else {
            root = unicode;
        }
        common_method();
    }

    public void jwt_web(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(2);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("jwt"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            jwt_web = root;
        } else {
            root = jwt_web;
        }
        common_method();
    }

    public void color_choose(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(3);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("color-choose"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            color_choose = root;
        } else {
            root = color_choose;
        }
        common_method();
    }

    public void qr_code(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(4);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("qr-code"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            qr_code = root;
        } else {
            root = qr_code;
        }
        common_method();
    }

    public void date_query(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(4);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("date"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            date_query = root;
        } else {
            root = date_query;
        }
        common_method();
    }

    public void cron(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(6);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("cron"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            cron = root;
        } else {
            root = cron;
        }
        common_method();
    }

    public void telephone(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(8);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("telephone"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            telephone = root;
        } else {
            root = telephone;
        }
        common_method();
    }


    private void json_view(boolean flag) {
        URL resource = this.getClass().getClassLoader().getResource("css/tree_item.css");
        splitPane.getScene().getStylesheets().add(resource.toExternalForm());
        //默认选择第一个
        listView.getSelectionModel().select(9);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("json_view"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            json_view = root;
        } else {
            root = json_view;
        }
        common_method();
    }

    private void maven_install_jar(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(10);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("maven-install-jar"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            maven_install_jar = root;
        } else {
            root = maven_install_jar;
        }
        common_method();
    }

    public void word_ocr(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(11);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("word-ocr"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            word_ocr = root;
        } else {
            root = word_ocr;
        }
        common_method();
    }

    public void bar_code(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(12);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("bar-code"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            bar_code = root;
        } else {
            root = bar_code;
        }
        common_method();
    }

    @FXML
    public void batch_update_file_name_menu_item() {
        boolean flag = false;
        if (batch_update_file_name != null) {
            flag = true;
        }

        batch_update_file_name(flag);
    }

    public void pdf_ocr(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(13);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("pdf-ocr"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            pdf_ocr = root;
        } else {
            root = pdf_ocr;
        }
        common_method();
    }

    //batch_update_file_name
    @FXML
    public void batch_update_file_name(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(14);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("batch_update_file_name"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            batch_update_file_name = root;
        } else {
            root = batch_update_file_name;
        }
        common_method();
    }

    public void capter_screen(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(15);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("capter-screen"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            batch_update_file_name = root;
        } else {
            root = batch_update_file_name;
        }
        common_method();
    }

    private void common_method() {
        splitPane.getItems().remove(1);
        splitPane.getItems().add(1, root);
        root.widthProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                double width = splitPane.getWidth();
                SmallToolsController.this.width.set(width);
                log.info("SmallTools:--->width:{}", width);
            }
        });
        root.heightProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                double height = splitPane.getHeight();
                SmallToolsController.this.height.set(height);
                log.info("SmallTools:--->height:{}", height);
            }
        });

        this.width.addListener((observable, oldValue, newValue) -> {
            SmallToolsController.this.root.setPrefWidth(newValue.doubleValue() - listView.getWidth());
            log.info("newValue.doubleValue():{}", newValue.doubleValue());
        });

        this.height.addListener((observable, oldValue, newValue) -> {
            SmallToolsController.this.root.setPrefHeight(newValue.doubleValue() - listView.getHeight());
            log.info("newValue.doubleValue():{}", newValue.doubleValue());
        });
    }

    @FXML
    public void mail_menu_item(ActionEvent event) {
        boolean flag = false;
        if (mail != null) {
            flag = true;
        }
        mail(flag);
    }

    private void mail(boolean flag) {
        //默认选择第一个
        listView.getSelectionModel().select(7);

        if (!flag) {
            try {
                root = FXMLLoader.load(ResourcesUtils.getResource("mail"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            mail = root;
        } else {
            root = mail;
        }
        common_method();
    }

    @FXML
    public void http_request_menu_item(ActionEvent event) {
        load_http_tools(0);
    }

    @FXML
    public void http_upload_menu_item(ActionEvent event) {
        load_http_tools(1);
    }

    @FXML
    public void http_download_menu_item(ActionEvent event) {
        load_http_tools(2);
    }

    public void load_http_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("http-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    @FXML
    public void http_server_menu_item(ActionEvent event) {
        load_server_tools(0);
    }

    @FXML
    public void ftp_server_menu_item(ActionEvent event) {
        load_server_tools(1);
    }

    @FXML
    public void ssh_client_menu_item(ActionEvent event) {
        load_server_tools(2);
    }

    public void load_server_tools(int index) {
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("server-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    @FXML
    public void socket_server_menu_item(ActionEvent event) {
        load_server_tools(3);
    }

    @FXML
    public void telephone_menu_item(ActionEvent event) {
        boolean flag = false;
        if (telephone != null) {
            flag = true;
        }
        telephone(flag);
    }

    @FXML
    public void mybatis_plus_gen_menu_item(ActionEvent event) {

        load_mysql_tools(1);
    }

    @FXML
    public void JsonView_menu_item(ActionEvent event) {
        boolean flag = false;
        if (json_view != null) {
            flag = true;
        }
        json_view(flag);
    }

    @FXML
    public void maven_jar_install_menu_item(ActionEvent event) {
        boolean flag = false;
        if (maven_install_jar != null) {
            flag = true;
        }
        maven_install_jar(flag);
    }

    @FXML
    public void word_ocr_menu_item(ActionEvent event) {
        boolean flag = false;
        if (word_ocr != null) {
            flag = true;
        }
        word_ocr(flag);
    }

    @FXML
    public void bar_code_menu_item(ActionEvent event) {
        boolean flag = false;
        if (bar_code != null) {
            flag = true;
        }
        bar_code(flag);
    }

    @FXML
    public void pdf_menu_item(ActionEvent event) {
        boolean flag = false;
        if (pdf_ocr != null) {
            flag = true;
        }
        pdf_ocr(flag);
    }

    public void capter_screen_menu_item(ActionEvent event) {
        boolean flag = false;
        if (capter_screen != null) {
            flag = true;
        }
        capter_screen(flag);
    }

    public void jks_file_menu_item(ActionEvent event) {
        load_encrypt(2);
    }

    public void music_parser_menu_item(ActionEvent event) {
        load_player(3);
    }

    public void json_javabean_gen_menu_item(ActionEvent event) {
        load_mysql_tools(2);
    }

    public void socket_client_aio_menu_item(ActionEvent event) {
        load_server_tools(4);
    }

    public void socket_server_aio_menu_item(ActionEvent event) {
        load_server_tools(5);
    }

    public void socket_server_nio_menu_item(ActionEvent event) {
        load_server_tools(6);
    }

    public void socket_client_nio_menu_item(ActionEvent event) {
        load_server_tools(7);
    }

    public void sql_query_gen_menu_item(ActionEvent event) {
        load_mysql_tools(3);
    }

    public void layui_form_gen_menu_item(ActionEvent actionEvent) {
        boolean flag =false;
        if (layui_form_gen != null){
            flag = true;
        }
        layui_form_gen(flag);
    }

    public void video_transcoder_menu_item(ActionEvent actionEvent) {
        load_player(4);
    }

    public void file_edit_menu_item(ActionEvent actionEvent) {
        boolean flag =false;
        if (file_edit != null){
            flag = true;
        }
        file_edit(flag);
    }

    public void log_console_menu_item(ActionEvent actionEvent) {
        boolean flag = false;
        if (log_console != null){
            flag = true;
        }
        log_console(flag);
    }

    public void edit_plus_code_menu_item(ActionEvent actionEvent) {

        boolean flag = false;
        if (editPlusCode != null){
            flag = true;
        }
        editPlusCode(flag);
    }

    public void minio_upload_menu_item(ActionEvent actionEvent) {
        boolean flag = false;
        if (minioUpload != null){
            flag = true;
        }
        minioUpload(flag);
    }

    public void excel_read_menu_item(ActionEvent actionEvent) {
        boolean flag = false;
        if (excel_read != null){
            flag = true;
        }
        excel_read(flag);
    }

    public void web_socket_client_menu_item(ActionEvent actionEvent) {
        load_server_tools(8);
    }

    public void load_go_tools(int index){
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("go-tools"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void json_to_struct_menu_item(ActionEvent actionEvent) {
        load_go_tools(0);
    }

    public void mysql_to_struct_menu_item(ActionEvent actionEvent) {
        load_go_tools(1);
    }

    public void js_edit_list(int index){
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("editor-list"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void ace_js_menu_item(ActionEvent actionEvent) {
        js_edit_list(0);
    }

    public void monaco_js_menu_item(ActionEvent actionEvent) {
        js_edit_list(1);
    }

    public void codemirror_js_menu_item(ActionEvent actionEvent) {
        js_edit_list(2);
    }

    public void timeFx_fx_menu_item(ActionEvent actionEvent) {
        js_edit_list(3);
    }

    public void swing_menu_item(ActionEvent actionEvent) {
        js_edit_list(4);
    }

    public void js_edit_list_impl(int index){
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("editor-list-impl"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

//        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
//        listView.getSelectionModel().select(index);
    }

    public void ace_menu_item(ActionEvent actionEvent) {
        js_edit_list_impl(0);
    }

    public void load_chat_app(int index){
        AnchorPane fx = null;
        try {
            fx = FXMLLoader.load(ResourcesUtils.getResource("chat-app"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        Scene scene = new Scene(fx);
        Stage stage = (Stage) splitPane.getScene().getWindow();
        stage.setScene(scene);

        ListView<ResourcesUtils.Player> listView = (ListView) fx.lookup("#listView");
        listView.getSelectionModel().select(index);
    }

    public void chat_app_menu_item(ActionEvent actionEvent) {
        load_chat_app(0);
    }
}
